package com.momochen.demo;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by momochen on 2016/8/5.
 */
public class MathUtils {
    private static long SENDTIME = 0L;
    private static final byte[] hex = "0123456789ABCDEF".getBytes();

    public static double[] BinaryEquation(double paramDouble1, double paramDouble2, double paramDouble3) {
        paramDouble3 = paramDouble2 * paramDouble2 - 4.0D * paramDouble1 * paramDouble3;
        if (paramDouble3 < 0.0D) {
            return null;
        }
        if (paramDouble3 == 0.0D) {
            paramDouble1 = -(paramDouble2 / (2.0D * paramDouble1));
            return new double[]{paramDouble1, paramDouble1};
        }
        return new double[]{(-paramDouble2 + Math.sqrt(paramDouble3)) / (2.0D * paramDouble1), (-paramDouble2 - Math.sqrt(paramDouble3)) / (2.0D * paramDouble1)};
    }

    public static String Bytes2HexString(byte[] paramArrayOfByte) {
        byte[] arrayOfByte = new byte[paramArrayOfByte.length * 2];
        int i = 0;
        while (i < paramArrayOfByte.length) {
            arrayOfByte[(i * 2)] = hex[(paramArrayOfByte[i] >> 4 & 0xF)];
            arrayOfByte[(i * 2 + 1)] = hex[(paramArrayOfByte[i] & 0xF)];
            i += 1;
        }
        return new String(arrayOfByte);
    }

    public static byte HexString2Byte(String paramString) {
        int i = 0 + 1;
        char c1 = paramString.charAt(0);
        char c2 = paramString.charAt(i);
        return (byte) (parse(c1) << 4 | parse(c2));
    }

    public static byte[] HexString2Bytes(String paramString) {
        byte[] arrayOfByte = new byte[paramString.length() / 2];
        int j = 0;
        int i = 0;
        while (i < arrayOfByte.length) {
            int k = j + 1;
            char c1 = paramString.charAt(j);
            j = k + 1;
            char c2 = paramString.charAt(k);
            arrayOfByte[i] = ((byte) (parse(c1) << 4 | parse(c2)));
            i += 1;
        }
        return arrayOfByte;
    }

    public static int KpatommHg(float paramFloat) {
        if (paramFloat > 0.0F) {
            return (int) (paramFloat * 7.5D);
        }
        return 0;
    }

    public static char byte2char(byte paramByte) {
        return (char) paramByte;
    }

    public static float bytes2float(byte[] paramArrayOfByte, int paramInt, boolean paramBoolean) {
        try {
            float f = getFloat(paramArrayOfByte, paramInt, paramBoolean);
            return f;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0.0F;
    }

    public static float bytes2float(byte[] paramArrayOfByte, boolean paramBoolean) {
        try {
            float f = getFloat(paramArrayOfByte, 0, paramBoolean);
            return f;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0.0F;
    }

    public static int bytes2int(byte[] paramArrayOfByte, int paramInt, boolean paramBoolean) {
        if (paramBoolean == true) {
            return (((paramArrayOfByte[(paramInt + 3)] & 0xFF) << 8 | paramArrayOfByte[(paramInt + 2)] & 0xFF) << 8 | paramArrayOfByte[(paramInt + 1)] & 0xFF) << 8 | paramArrayOfByte[(paramInt + 0)] & 0xFF;
        }
        return (((paramArrayOfByte[(paramInt + 0)] & 0xFF) << 8 | paramArrayOfByte[(paramInt + 1)] & 0xFF) << 8 | paramArrayOfByte[(paramInt + 2)] & 0xFF) << 8 | paramArrayOfByte[(paramInt + 3)] & 0xFF;
    }

    public static int bytes2int(byte[] paramArrayOfByte, boolean paramBoolean) {
        if (paramBoolean == true) {
            return (((paramArrayOfByte[3] & 0xFF) << 8 | paramArrayOfByte[2] & 0xFF) << 8 | paramArrayOfByte[1] & 0xFF) << 8 | paramArrayOfByte[0] & 0xFF;
        }
        return (((paramArrayOfByte[0] & 0xFF) << 8 | paramArrayOfByte[1] & 0xFF) << 8 | paramArrayOfByte[2] & 0xFF) << 8 | paramArrayOfByte[3] & 0xFF;
    }

    public static short bytes2short(byte[] paramArrayOfByte, int paramInt, boolean paramBoolean) {
        int i = (short) (paramArrayOfByte[(paramInt + 0)] & 0xFF);
        paramInt = (short) (paramArrayOfByte[(paramInt + 1)] & 0xFF);
        if (paramBoolean == true) {
            return (short) (i | (short) (paramInt << 8));
        }
        return (short) ((short) (i << 8) | paramInt);
    }

    public static short bytes2short(byte[] paramArrayOfByte, boolean paramBoolean) {
        int i = (short) (paramArrayOfByte[0] & 0xFF);
        int j = (short) (paramArrayOfByte[1] & 0xFF);
        if (paramBoolean == true) {
            j = (short) (j << 8);
        }

        return (short) (i | j);
    }

    public static boolean checkBoolsIsSame(boolean[] paramArrayOfBoolean, boolean paramBoolean) {
        int i = 0;
        while (i < paramArrayOfBoolean.length) {
            if (paramArrayOfBoolean[i] != paramBoolean) {
                return false;
            }
            i += 1;
        }
        return true;
    }

    public static short[] compress_bytes2short(byte[] paramArrayOfByte, int paramInt, boolean paramBoolean) {
        short[] arrayOfShort = new short[2];
        int j = (short) (paramArrayOfByte[(paramInt + 0)] & 0xFF);
        int i = (short) (paramArrayOfByte[(paramInt + 1)] & 0xFF);
        paramInt = (short) (paramArrayOfByte[(paramInt + 2)] & 0xFF);
        if (paramBoolean == true) {
            arrayOfShort[0] = ((short) (j | (short) ((short) (i & 0xF0) << 4)));
            if (arrayOfShort[0] > 2048) {
                arrayOfShort[0] = ((short) (arrayOfShort[0] - 4096));
            }
            arrayOfShort[1] = ((short) (paramInt | (short) ((short) (i & 0xF) << 8)));
            if (arrayOfShort[1] > 2048) {
                arrayOfShort[1] = ((short) (arrayOfShort[1] - 4096));
            }
            return arrayOfShort;
        }
        paramInt = (short) (i & 0xF0);
        paramInt = (short) (j << 4);
        arrayOfShort[0] = 0;
        paramInt = (short) ((short) (i & 0xF) << 8);
        arrayOfShort[1] = 0;
        return arrayOfShort;
    }

    public static float degreeToRound(float paramFloat) {
        return (float) (0.0174532925199433D * paramFloat);
    }

    public static void float2bytes(float paramFloat, byte[] paramArrayOfByte, int paramInt, boolean paramBoolean) {
        int2bytes(Float.floatToIntBits(paramFloat), paramArrayOfByte, paramInt, paramBoolean);
    }

    public static byte[] float2bytes(float paramFloat, boolean paramBoolean) {
        return int2bytes(Float.floatToIntBits(paramFloat), paramBoolean);
    }

    public static String getDateString(Date paramDate) {
        String str = Integer.toString(paramDate.getYear() + 1900);
        str = str + String.format("%02d", new Object[]{Integer.valueOf(paramDate.getMonth() + 1)});
        return str + String.format("%02d", new Object[]{Integer.valueOf(paramDate.getDate())});
    }

    public static int getDayStartTime(int paramInt) {
        Calendar localCalendar = Calendar.getInstance();
        localCalendar.setTime(new Date(paramInt * 1000L));
        return (int) (new Date(localCalendar.get(Calendar.YEAR) - 1900, localCalendar.get(Calendar.MONTH), localCalendar.get(Calendar.DAY_OF_MONTH)).getTime() / 1000L);
    }

    public static float getDrawTime(long paramLong) {
        Calendar localCalendar = Calendar.getInstance();
        localCalendar.setTime(new Date(1000L * paramLong));
        localCalendar.get(Calendar.YEAR);
        localCalendar.get(Calendar.MONTH);
        localCalendar.get(Calendar.DAY_OF_MONTH);
        int i = localCalendar.get(Calendar.HOUR_OF_DAY);
        int j = localCalendar.get(Calendar.MINUTE);
        return i + j / 60.0F;
    }

    public static float getFactor(float paramFloat1, float paramFloat2, float paramFloat3, float paramFloat4) {
        return (paramFloat4 - paramFloat2) / (paramFloat2 - paramFloat1);
    }

    public static String getFileTotalTimeString(long paramLong) {
        return new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(new Date(paramLong));
    }

    public static float getFloat(byte[] paramArrayOfByte, int paramInt, boolean paramBoolean)
            throws Exception {
        if (paramBoolean == true) {
        }
        for (paramInt = (((paramArrayOfByte[(paramInt + 3)] & 0xFF) << 8 | paramArrayOfByte[(paramInt + 2)] & 0xFF) << 8 | paramArrayOfByte[(paramInt + 1)] & 0xFF) << 8 | paramArrayOfByte[(paramInt + 0)] & 0xFF; ; paramInt = (((paramArrayOfByte[(paramInt + 0)] & 0xFF) << 8 | paramArrayOfByte[(paramInt + 1)] & 0xFF) << 8 | paramArrayOfByte[(paramInt + 2)] & 0xFF) << 8 | paramArrayOfByte[(paramInt + 3)] & 0xFF) {
            return Float.intBitsToFloat(paramInt);
        }
    }

    public static long getHandleTime() {
        return System.currentTimeMillis() - SENDTIME;
    }

//    public static long getLongTime(int paramInt1, int paramInt2, int paramInt3) {
//        Calendar localCalendar = Calendar.getInstance();
//        localCalendar.set(1, paramInt1);
//        localCalendar.set(2, paramInt2);
//        localCalendar.set(5, paramInt3);
//        return localCalendar.getTimeInMillis();
//    }

//    public static long getLongTimeByStr(String paramString1, String paramString2) {
//        paramString2 = new SimpleDateFormat(paramString2);
//        try {
//            long l = paramString2.parse(paramString1).getTime();
//            return l;
//        } catch (ParseException paramString1) {
//            paramString1.printStackTrace();
//        }
//        return 0L;
//    }

    public static String getMouthAndDay(long paramLong) {
        return new SimpleDateFormat("MM/dd").format(new Date(paramLong * 1000L));
    }

    public static String getSampingTimeString(int paramInt1, int paramInt2) {
        paramInt1 /= paramInt2;
        long l1 = paramInt1 % 60;
        long l2 = paramInt1 / 60;
        return String.format("%02d:%02d:%02d", new Object[]{Long.valueOf(l2 / 60L), Long.valueOf(l2 % 60L), Long.valueOf(l1)});
    }

    public static String getStartEndTimeByLong(long paramLong) {
        Object localObject = Calendar.getInstance();
        ((Calendar) localObject).setTimeInMillis(paramLong);
        localObject = new Date(((Calendar) localObject).get(Calendar.YEAR) - 1900, ((Calendar) localObject).get(Calendar.MONTH), ((Calendar) localObject).get(Calendar.DAY_OF_MONTH), ((Calendar) localObject).get(Calendar.HOUR_OF_DAY), ((Calendar) localObject).get(Calendar.MINUTE));
        return new SimpleDateFormat("yyyy/MM/dd").format((Date) localObject);
    }

    public static String getStrTimeByLong(long paramLong, String paramString) {
        if (paramLong == 0L) {
            return "";
        }
        try {
            paramString = new SimpleDateFormat(paramString).format(new Date(paramLong));
            return paramString;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getStringTime(long paramLong) {
        Date localDate = new Date(paramLong);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(localDate);
    }

//    public static String getStringToHex(byte[] paramArrayOfByte) {
//        StringBuffer localStringBuffer = new StringBuffer();
//        int i = 0;
//        if (i < paramArrayOfByte.length) {
//            int j = (char) (paramArrayOfByte[i] >> 4 & 0xF);
//            int k = (char) (paramArrayOfByte[i] & 0xF);
//            char c1;
//            if ((j >= 0) && (j <= 9)) {
//                c1 = (char) (j + 48);
//                label56:
//                if ((k < 0) || (k > 9)) {
//                    break label103;
//                }
//            }
//            label103:
//            for (char c2 = (char) (k + 48); ; c2 = (char) (k + 87)) {
//                localStringBuffer.append(c1).append(c2);
//                i += 1;
//                break;
//                c1 = (char) (j + 87);
//                break label56;
//            }
//        }
//        return localStringBuffer.toString();
//    }
//
//    public static String getStringToHexSperater(byte[] paramArrayOfByte, char paramChar) {
//        StringBuffer localStringBuffer = new StringBuffer();
//        int i = 0;
//        if (i < paramArrayOfByte.length) {
//            int j = (char) (paramArrayOfByte[i] >> 4 & 0xF);
//            int k = (char) (paramArrayOfByte[i] & 0xF);
//            char c1;
//            if ((j >= 0) && (j <= 9)) {
//                c1 = (char) (j + 48);
//                label60:
//                if ((k < 0) || (k > 9)) {
//                    break label113;
//                }
//            }
//            label113:
//            for (char c2 = (char) (k + 48); ; c2 = (char) (k + 87)) {
//                localStringBuffer.append(c1).append(c2).append(paramChar);
//                i += 1;
//                break;
//                c1 = (char) (j + 87);
//                break label60;
//            }
//        }
//        return localStringBuffer.toString();
//    }
//
//    public static String getTime(long paramLong) {
//        Calendar localCalendar = Calendar.getInstance();
//        localCalendar.setTimeInMillis(paramLong);
//        int i = localCalendar.get(11);
//        int j = localCalendar.get(12);
//        int k = localCalendar.get(13);
//        return parseZero(i) + ":" + parseZero(j) + ":" + parseZero(k);
//    }

    public static long getTimeByStr2(int paramInt1, int paramInt2, int paramInt3, int paramInt4, int paramInt5) {
        return new Date(paramInt1 - 1900, paramInt2, paramInt3, paramInt4, paramInt5).getTime();
    }

    public static long getTimeBystr(int paramInt1, int paramInt2, int paramInt3) {
        return new Date(paramInt1 - 1900, paramInt2, paramInt3).getTime();
    }

    public static String getTimeSecondString(long paramLong) {
        paramLong /= 1000L;
        long l = paramLong / 60L;
        return String.format("%02d:%02d:%02d", new Object[]{Long.valueOf(l / 60L), Long.valueOf(l % 60L), Long.valueOf(paramLong % 60L)});
    }

    public static String getTimeString(long paramLong) {
        paramLong /= 1000L;
        long l = paramLong / 60L;
        return String.format("%02d:%02d:%02d", new Object[]{Long.valueOf((l / 60L + 8L) % 24L), Long.valueOf(l % 60L), Long.valueOf(paramLong % 60L)});
    }

    public static String getTimeString(Date paramDate) {
        return new SimpleDateFormat("HHmmss").format(paramDate);
    }

    public static String getTotalTimeString(long paramLong) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(paramLong));
    }

    public static String getTotalTimeString2(long paramLong) {
        return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date(paramLong));
    }

    public static void int2bytes(int paramInt1, byte[] paramArrayOfByte, int paramInt2, boolean paramBoolean) {
        if (paramBoolean == true) {
            paramArrayOfByte[(paramInt2 + 0)] = ((byte) (paramInt1 & 0xFF));
            paramArrayOfByte[(paramInt2 + 1)] = ((byte) ((0xFF00 & paramInt1) >> 8));
            paramArrayOfByte[(paramInt2 + 2)] = ((byte) ((0xFF0000 & paramInt1) >> 16));
            paramArrayOfByte[(paramInt2 + 3)] = ((byte) ((0xFF000000 & paramInt1) >> 24));
            return;
        }
        paramArrayOfByte[(paramInt2 + 3)] = ((byte) (paramInt1 & 0xFF));
        paramArrayOfByte[(paramInt2 + 2)] = ((byte) ((0xFF00 & paramInt1) >> 8));
        paramArrayOfByte[(paramInt2 + 1)] = ((byte) ((0xFF0000 & paramInt1) >> 16));
        paramArrayOfByte[(paramInt2 + 0)] = ((byte) ((0xFF000000 & paramInt1) >> 24));
    }

    public static byte[] int2bytes(int paramInt, boolean paramBoolean) {
        byte[] arrayOfByte = new byte[4];
        if (paramBoolean == true) {
            arrayOfByte[0] = ((byte) (paramInt & 0xFF));
            arrayOfByte[1] = ((byte) ((0xFF00 & paramInt) >> 8));
            arrayOfByte[2] = ((byte) ((0xFF0000 & paramInt) >> 16));
            arrayOfByte[3] = ((byte) ((0xFF000000 & paramInt) >> 24));
            return arrayOfByte;
        }
        arrayOfByte[3] = ((byte) (paramInt & 0xFF));
        arrayOfByte[2] = ((byte) ((0xFF00 & paramInt) >> 8));
        arrayOfByte[1] = ((byte) ((0xFF0000 & paramInt) >> 16));
        arrayOfByte[0] = ((byte) ((0xFF000000 & paramInt) >> 24));
        return arrayOfByte;
    }

//    public static boolean isNumber(String paramString) {
//        if (paramString.length() == 0) {
//            return false;
//        }
//        int i = 0;
//        for (; ; ) {
//            if (i >= paramString.length()) {
//                break label37;
//            }
//            if (!Character.isDigit(paramString.charAt(i))) {
//                break;
//            }
//            i += 1;
//        }
//        label37:
//        return true;
//    }
//
//    public static byte[] long2bytes(long paramLong, boolean paramBoolean) {
//        byte[] arrayOfByte = new byte[8];
//        if (paramBoolean == true) {
//            i = 0;
//            while (i < 8) {
//                arrayOfByte[i] = ((byte) (int) (paramLong >> 56 - i * 8));
//                i += 1;
//            }
//        }
//        int i = 0;
//        while (i < 8) {
//            arrayOfByte[(7 - i)] = ((byte) (int) (paramLong >> 56 - i * 8));
//            i += 1;
//        }
//        return arrayOfByte;
//    }
//
//    public static void main(String[] paramArrayOfString) {
//        paramArrayOfString = Calendar.getInstance();
//        paramArrayOfString.setTime(new Date());
//        paramArrayOfString.set(5, 3);
//        long l = paramArrayOfString.getTimeInMillis();
//        System.out.println("time=" + l);
//    }

    public static float mmHgtoKpa(int paramInt) {
        if (paramInt > 0) {
            return (int) ((float) (paramInt / 7.5D) * 10.0F) / 10.0F;
        }
        return 0.0F;
    }

    private static int parse(char paramChar) {
        if (paramChar >= 'a') {
            return paramChar - 'a' + 10 & 0xF;
        }
        if (paramChar >= 'A') {
            return paramChar - 'A' + 10 & 0xF;
        }
        return paramChar - '0' & 0xF;
    }

    private static String parseZero(int paramInt) {
        if (paramInt < 10) {
            return "0" + String.valueOf(paramInt);
        }
        return String.valueOf(paramInt);
    }

    public static void setSendTime(long paramLong) {
        SENDTIME = paramLong;
    }

    public static void short2bytes(short paramShort, byte[] paramArrayOfByte, int paramInt, boolean paramBoolean) {
        if (paramBoolean == true) {
            paramArrayOfByte[(paramInt + 0)] = ((byte) (paramShort & 0xFF));
            paramArrayOfByte[(paramInt + 1)] = ((byte) (paramShort >> 8 & 0xFF));
            return;
        }
        paramArrayOfByte[(paramInt + 1)] = ((byte) (paramShort & 0xFF));
        paramArrayOfByte[(paramInt + 0)] = ((byte) (paramShort >> 8 & 0xFF));
    }

    public static byte[] short2bytes(short paramShort, boolean paramBoolean) {
        byte[] arrayOfByte = new byte[2];
        if (paramBoolean == true) {
            arrayOfByte[0] = ((byte) (paramShort & 0xFF));
            arrayOfByte[1] = ((byte) (paramShort >> 8 & 0xFF));
            return arrayOfByte;
        }
        arrayOfByte[1] = ((byte) (paramShort & 0xFF));
        arrayOfByte[0] = ((byte) (paramShort >> 8 & 0xFF));
        return arrayOfByte;
    }

    public static short[] ti_compress_bytes2short(byte[] paramArrayOfByte, int paramInt, boolean paramBoolean) {
        short[] arrayOfShort = new short[2];
        int j = (short) (paramArrayOfByte[(paramInt + 0)] & 0xFF);
        int i = (short) (paramArrayOfByte[(paramInt + 1)] & 0xFF);
        int k = (short) (paramArrayOfByte[(paramInt + 2)] & 0xFF);
        k = (short) (paramArrayOfByte[(paramInt + 3)] & 0xFF);
        int m = (short) (paramArrayOfByte[(paramInt + 4)] & 0xFF);
        paramInt = (short) (paramArrayOfByte[(paramInt + 5)] & 0xFF);
        if (paramBoolean == true) {
            arrayOfShort[0] = ((short) (j * 256 + i));
            arrayOfShort[1] = ((short) (k * 256 + m));
            return arrayOfShort;
        }
        paramInt = (short) (i & 0xF0);
        paramInt = (short) (j << 4);
        arrayOfShort[0] = 0;
        paramInt = (short) ((short) (i & 0xF) << 8);
        arrayOfShort[1] = 0;
        return arrayOfShort;
    }

    public static byte xorAdd(byte[] paramArrayOfByte) {
        byte b = paramArrayOfByte[0];
        int i = 1;
        while (i < paramArrayOfByte.length) {
            b = (byte) (paramArrayOfByte[i] ^ b);
            i += 1;
        }
        return b;
    }
}
